//
//  ZYCBaseViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/5/14.
//

#import "ZYCBaseViewController.h"
#import "ZYCNoDataView.h"
#import "ZYCLoginViewController.h"
#import "UIViewController+BackButtonHandler.h"
#import "ZYCMessageRootViewController.h"
#import "ZYCNavigationController.h"

@interface ZYCBaseViewController ()

@property (nonatomic, strong) ZYCNoDataView *noDataView;

@end

@implementation ZYCBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.automaticallyAdjustsScrollViewInsets = YES;//禁止自动调整,会导致页面下移
    //设置背景色 防止卡顿
    self.view.backgroundColor = [UIColor whiteColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(penetrateNoti:) name:@"kPenetrateMessage" object:nil];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"";
    self.navigationController.navigationBar.backIndicatorImage = [UIImage imageNamed:@"common_back"];
    self.navigationController.navigationBar.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"common_back"];
    self.navigationItem.backBarButtonItem = backItem;
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
//    UIImage *image = [UIImage imageNamed:@"Path"];
//    if (self.navigationController.childViewControllers.count > 1) {
//        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
//    }else{
//        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[UIView new]];
//    }
//    [self.navigationController.navigationBar setTintColor:[UIColor redColor]];

}

- (void)back:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}


// 处理透传消息
- (void)penetrateNoti:(NSNotification *)noti{
    NSDictionary *notiDict = noti.object;
//    NSDictionary *message = [self dictionaryWithJsonString:notiDict[@"payload"]];
//    NSLog(@"通知消息:%@",message);
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"kLoginFlag"]){
//        ZYCMessageRootViewController *vc = [[ZYCMessageRootViewController alloc] init];
//        [self.navigationController pushViewController:vc animated:YES];
    }else{
//        ZYCLoginViewController *vc = [[ZYCLoginViewController alloc] init];
//        [self.navigationController pushViewController:vc animated:YES];
        
        ZYCLoginViewController *vc = [[ZYCLoginViewController alloc]init];
        ZYCNavigationController *nav = [[ZYCNavigationController alloc]initWithRootViewController:vc];
        [[[UIApplication sharedApplication] delegate] window].rootViewController = nav;
        [[[[UIApplication sharedApplication] delegate] window] makeKeyAndVisible];
        
    }
}

-(NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString{
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

- (BOOL)navigationShouldPopOnBackButton{
    return YES;
}



- (ZYCNoDataView *)noDataView {
    if (!_noDataView) {
        _noDataView = [[ZYCNoDataView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight)];
    }
    return _noDataView;
}

- (void)showNoDataView{
    [self.view addSubview:self.noDataView];
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
